<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.client.methods.CloseableHttpResponse" %>
<%@ page import="org.apache.http.client.methods.HttpPost" %>
<%@ page import="org.apache.http.entity.StringEntity" %>
<%@ page import="org.apache.http.impl.client.CloseableHttpClient" %>
<%@ page import="org.apache.http.impl.client.HttpClientBuilder" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="org.dom4j.Document" %>
<%@ page import="org.dom4j.DocumentHelper" %>
<%@ page import="org.dom4j.Element" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.util.List" %>
<%
    String wsdl = "http://172.16.8.7:19000/";
    String soapAction = "http://tempuri.org/IMainService/cud";
    String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
            "   <soapenv:Header/>\n" +
            "   <soapenv:Body>\n" +
            "      <tem:cud>\n" +
            "         <!--Optional:-->\n" +
            "         <tem:cmpKey>test</tem:cmpKey>\n" +
            "         <!--Optional:-->\n" +
            "         <tem:typeid>1470000113</tem:typeid>\n" +
            "         <!--Optional:-->\n" +
            "         <tem:tables>&lt;Documents&gt;&lt;row&gt;&lt;DocDate&gt;20220426&lt;/DocDate&gt;&lt;RequriedDate&gt;20211130&lt;/RequriedDate&gt;&lt;Comments&gt;备注&lt;/Comments&gt;&lt;U_FWuser&gt;张三-生产车间&lt;/U_FWuser&gt;&lt;U_FWprocess&gt;33&lt;/U_FWprocess&gt;&lt;/row&gt;&lt;/Documents&gt;&lt;Document_Lines&gt;&lt;row&gt;&lt;ItemCode&gt;CPL02111&lt;/ItemCode&gt;&lt;Quantity&gt;500&lt;/Quantity&gt;&lt;RequiredDate&gt;20220611&lt;/RequiredDate&gt;&lt;/row&gt;&lt;/Document_Lines&gt;</tem:tables>\n" +
            "         <!--Optional:-->\n" +
            "         <tem:key></tem:key>\n" +
            "      </tem:cud>\n" +
            "   </soapenv:Body>\n" +
            "</soapenv:Envelope>";
    String message = doPostSoap( wsdl, xml, soapAction );
    out.print( "message" + message );
%>
<%!
    /***
     *
     * @param url
     * @param soapXml
     * @param soapAction
     * @return
     * @throws Exception
     */
    public String doPostSoap(String url, String soapXml, String soapAction) {
        BaseBean log = new BaseBean();
        log.writeLog( "url:" + url );
        log.writeLog( "soapXml:" + soapXml );
        log.writeLog( "soapAction:" + soapAction );
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost( url );
        //设置请求和传输超时时间
        String returnResult = "";
        CloseableHttpResponse response = null;
        try {
            httpPost.setHeader( "Content-Type", "text/xml;charset=UTF-8" );
            httpPost.setHeader( "SOAPAction", soapAction );
            StringEntity data = new StringEntity( soapXml );
            log.writeLog( "data:" + data.toString() );
            httpPost.setEntity( data );
            response = closeableHttpClient.execute( httpPost );
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
               String result = EntityUtils.toString( httpEntity, "UTF-8" );
                log.writeLog( "result:" + result );
                if (!"".equals( result )) {
                        Document xmlDocument = DocumentHelper.parseText( result );
                        if (xmlDocument != null) {
                            Element rootElement = xmlDocument.getRootElement();
                            Element body = rootElement.element( "Body" );
                            Element cudResponse = body.element( "cudResponse" );
                            Element cudResult = cudResponse.element( "cudResult" );
                            List<Element> listElement = cudResult.elements();
                            for (Element e : listElement) {//遍历所有一级子节点
                                String str = e.getTextTrim();
                                returnResult +="".equals( returnResult )?str:","+str;
                            }
                        }
                } else {
                    returnResult = "接口返回结果为空";
                }
            } else {
                returnResult = "sap接口异常";
            }
        } catch (Exception e) {
            log.writeLog( "e：" + e );
        } finally {
            if (response != null) {
                try {
                    EntityUtils.toString( response.getEntity() );
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return returnResult;
    }
%>